package cn.psvmc.cxzapi.controller.goods;

import cn.psvmc.cxzapi.model.common.*;
import cn.psvmc.cxzapi.model.goods.*;
import cn.psvmc.cxzapi.response.*;
import cn.psvmc.cxzapi.service.goods.TGoodsService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品管理
 *
 * @Author auto
 * @Date 2024-08-13
 */
@RestController
@RequestMapping("/goods")
public class TGoodsController {

    @Resource
    private TGoodsService tGoodsService;

    /**
     * @Description 保存商品类目信息
     * @Date 2024/8/14 8:50
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/step1")
    public Result step1(@RequestBody TGoods tGoods) {
        ToolUtils.validation(new Object[]{
                tGoods.getStoreid(), "商家id", 1
                , tGoods.getSortId(), "分类id", 1
                , tGoods.getSortName(), "分类名", 1
        });
        return tGoodsService.step1(tGoods);
    }

    /**
     * @Description 保存商品信息
     * @Date 2024/8/14 9:05
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/step2")
    public Result step2(@RequestBody TGoods tGoods) {
        ToolUtils.validation(new Object[]{
                tGoods.getGoodsId(), "商品id", 1
                , tGoods.getName(), "商品名", 2
                , tGoods.getCoverImg(), "封面图", 2
        });
        return tGoodsService.step2(tGoods);
    }

    /**
     * @Description 保存销售信息
     * @Date 2024/8/14 9:15
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/step3")
    public Result step3(@RequestBody TGoods tGoods) {
        ToolUtils.validation(new Object[]{
                tGoods.getGoodsId(), "商品id", 1
                , tGoods.getRents(), "租赁方案", 3
                , tGoods.getAttributes(), "商品属性", 3
                , tGoods.getSpecs(), "销售规格", 3
        });
        return tGoodsService.step3(tGoods);
    }

    /**
     * @Description 保存物流信息
     * @Date 2024/8/14 9:36
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/step4")
    public Result step4(@RequestBody TGoodsLogistics tGoods) {
        ToolUtils.validation(new Object[]{
                tGoods.getGoodsId(), "商品id", 1
                , tGoods.getOutAddressid(), "发货地址id", 1
                , tGoods.getInAddressid(), "归还地址id", 1
                , tGoods.getSendOut(), "寄出快递", 2
                , tGoods.getOutType(), "配送方式(寄出)", 1
                , tGoods.getInType(), "配送方式(寄回)", 1
        });
        return tGoodsService.step4(tGoods);
    }

    /**
     * @Description 提交审核
     * @Date 2024/8/26 17:35
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/commit")
    public Result commit(@RequestBody DetailEntity entity) {
        Integer goodsId = entity.getId();
        ToolUtils.validation(new Object[]{
                goodsId, "商品id", 1
        });
        return tGoodsService.commit(goodsId);
    }

    /**
     * @Author auto 2024-08-13 查询商品表详情
     * @Param tGoods
     * @Return cn.psvmc.cxzapi.response.Result
     */
    @PostMapping("/detail")
    public Result detail(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tGoodsService.detail(id));
    }

    /**
     * @Description 销售信息
     * @Date 2024/8/14 9:49
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail3")
    public Result detail3(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tGoodsService.detail3(id));
    }

    /**
     * @Description 物流信息
     * @Date 2024/8/14 9:58
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail4")
    public Result detail4(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tGoodsService.detail4(id));
    }

    /**
     * @Description 商品二维码
     * @Date 2024/8/14 17:02
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail_code")
    public Result detailCode(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tGoodsService.detailCode(id));
    }

    /**
     * @Author auto 2024-08-13 查询商品表列表
     * @Param entity 分页查询通用实体类
     * @Return cn.psvmc.cxzapi.response.Result
     */
    @PostMapping("/list")
    public PageResult2 list(@RequestBody SearchEntity entity) {
        Page<TGoods> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tGoodsService.list(queryPage, entity));
    }

    @PostMapping("/list_pt")
    public PageResult2 listPt(@RequestBody SearchEntity entity) {
        Page<TGoods> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tGoodsService.listPt(queryPage, entity));
    }

    /**
     * @Description 下架
     * @Date 2024/8/13 22:26
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/down")
    public Result down(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        tGoodsService.updateStatus(id, 2);
        return ResultGenerator.genSuccessResult("下架成功！");
    }

    /**
     * @Description 平台下架商品
     * @Date 2024/10/28 22:53
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/plateform_down")
    public Result plateformDown(@RequestBody TGoods tGoods) {
        ToolUtils.validation(new Object[]{tGoods.getGoodsId(), "id", 1, tGoods.getRversion(), "版本", 1, tGoods.getCheckComm(), "下架原因", 2});
        tGoodsService.plateformDown(tGoods);
        return ResultGenerator.genSuccessResult("下架成功！");
    }

    /**
     * @Description 审核
     * @Date 2024/8/13 22:34
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/check")
    public Result check(@RequestBody TGoods tGoods) {
        ToolUtils.validation(new Object[]{tGoods.getGoodsId(), "id", 1, tGoods.getRversion(), "版本", 1, tGoods.getCheckStatus(), "审核状态", 1});
        tGoodsService.check(tGoods);
        return ResultGenerator.genSuccessResult("更新成功！");
    }

    /**
     * @Description 撤销审核
     * @Date 2024/10/2 20:44
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/cancel_check")
    public Result cancelCheck(@RequestBody TGoods tGoods) {
        Integer goodsId = tGoods.getGoodsId();
        ToolUtils.validation(new Object[]{goodsId, "id", 1});
        tGoodsService.cancelCheck(goodsId);
        return ResultGenerator.genSuccessResult("撤销成功！");
    }

    /**
     * @Description 编辑总库存
     * @Date 2024/8/14 15:07
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/edit_stock")
    public Result editStock(@RequestBody TGoods tGoods) {
        Integer goodsId = tGoods.getGoodsId();
        List<TGoodsSpecs> specs = tGoods.getSpecs();
        ToolUtils.validation(new Object[]{goodsId, "商品id", 1, specs, "总库存", 3});
        tGoodsService.editStock(goodsId, specs);
        return ResultGenerator.genSuccessResult("更新成功！");
    }

    /**
     * @Description 获取所有商品标签
     * @Date 2024/8/17 15:45
     * @Author YWX
     * @Param []
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_tag")
    public Result listTag() {
        return ResultGenerator.genSuccessResult("更新成功！", tGoodsService.listTag());
    }

    /**
     * @Description 添加商品标签
     * @Date 2024/8/17 15:52
     * @Author YWX
     * @Param [vo]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/add_tag")
    public Result addTag(@RequestBody TGoodsTagVo vo) {
        ToolUtils.validation(new Object[]{vo.getGoodsId(), "商品id", 1, vo.getTags(), "商品标签", 3});
        tGoodsService.addTag(vo);
        return ResultGenerator.genSuccessResult("保存成功！");
    }

    /**
     * @Description 商品选择列表
     * @Date 2024/8/17 22:22
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_sel")
    public PageResult2 listForSel(@RequestBody SearchEntity entity) {
        Page<TGoods> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tGoodsService.listForSel(queryPage, entity));
    }

    /**
     * @Description 使用的分类列表
     * @Date 2024/8/24 15:11
     * @Author YWX
     * @Param []
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_used_type")
    public Result listUsedType() {
        return ResultGenerator.genSuccessResult("获取成功！", tGoodsService.listUsedType());
    }

    /**
     * @Description 删除
     * @Date 2024/8/30 22:28
     * @Author YWX
     * @Param [tUser]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/delete")
    public Result delete(@RequestBody DelEntity tUser) {
        Integer id = tUser.getId();
        Integer rversion = tUser.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        int delResult = tGoodsService.delete(id, rversion);
        if (delResult > 0) {
            return ResultGenerator.genSuccessResult("删除成功！");
        }
        return ResultGenerator.genFailResult("删除失败！");
    }

    /**
     * @Description 根据商品id获取库存
     * @Date 2024/9/19 21:29
     * @Author YWX
     * @Param [vo]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_stock")
    public Result listStock(@RequestBody TGoodsTagVo vo) {
        Integer goodsId = vo.getGoodsId();
        ToolUtils.validation(new Object[]{goodsId, "商品id", 1});
        return ResultGenerator.genSuccessResult("获取成功！", tGoodsService.listStock(goodsId));
    }

    /**
     * @Description 复制商品
     * @Date 2024/9/20 13:05
     * @Author YWX
     * @Param [vo]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/copy_goods")
    public Result copyGoods(@RequestBody TGoodsTagVo vo) {
        Integer goodsId = vo.getGoodsId();
        ToolUtils.validation(new Object[]{goodsId, "商品id", 1});
        tGoodsService.copyGoods(goodsId);
        return ResultGenerator.genSuccessResult("复制成功！");
    }

    /**
     * @Description 根据商品id获取商品列表
     * @Date 2024/9/28 20:42
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_goods_by_ids")
    public Result listGoodsByIds(@RequestBody GoodsEntity entity) {
        ToolUtils.validation(new Object[]{entity.getGoodsIds(), "商品ids", 2});
        entity.setSort(1);
        return ResultGenerator.genSuccessResult(tGoodsService.listGoodsByIds(entity));
    }

    /**
     * @Description 更新是否是白名单商品
     * @Date 2024/10/10 21:54
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/edit_is_white")
    public Result editIsWhite(@RequestBody TGoods tGoods) {
        ToolUtils.validation(new Object[]{tGoods.getGoodsId(), "id", 1, tGoods.getRversion(), "版本", 1});
        Integer isWhite = tGoods.getIsWhite();
        if (isWhite == null || (isWhite != 0 && isWhite != 1)) {
            return ResultGenerator.genFailResult("请选择是否是白名单商品！");
        }
        tGoodsService.editIsWhite(tGoods);
        return ResultGenerator.genSuccessResult("更新成功！");
    }

    /**
     * @Description 根据商品id更新商品类型
     * @Date 2024/12/14 23:11
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/update_type")
    public Result updateGoodsType(@RequestBody TGoods tGoods) {
        Integer goodsId = tGoods.getGoodsId();
        Integer goodsType = tGoods.getGoodsType();
        ToolUtils.validation(new Object[]{goodsId, "id", 1, goodsType, "类型", 2});
        tGoodsService.updateGoodsType(goodsId, goodsType);
        return ResultGenerator.genSuccessResult("更新成功！");
    }
}
